package net.xxcxcxcx.xc_enhancedmob.skills.basics.individual.rare;

import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.damage.ProjectileDamageSource;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.projectile.PersistentProjectileEntity;
import net.minecraft.nbt.NbtCompound;
import net.xxcxcxcx.xc_enhancedmob.config.skill.SkillConfig;
import net.xxcxcxcx.xc_enhancedmob.skills.BaseSkill;
import net.xxcxcxcx.xc_enhancedmob.skills.Ref;
import net.xxcxcxcx.xc_enhancedmob.skills.SkillRarity;

public class ReboundSkill extends BaseSkill {

    public ReboundSkill(int level) {
        super(level);
    }

    public ReboundSkill(NbtCompound nbtCompound) {
        super(nbtCompound);
    }

    public ReboundSkill() {
    }

    @Override
    public SkillConfig getDefaultConfig() {
        SkillConfig skillConfig = super.getDefaultConfig();
        skillConfig.setMessage(getConfigMessage(skillConfig));
        return skillConfig;
    }

    @Override
    public String getConfigMessage(SkillConfig skillConfig) {
        if (skillConfig == null) return super.getConfigMessage(null);
        return String.format("反弹技能，反射弹射物。");
    }

    public SkillRarity getSkillRarity() {
        return SkillRarity.Rare;
    }

    @Override
    public boolean onDamaged(LivingEntity attacker, MobEntity target, DamageSource source, Ref<Float> amount, boolean ismob, boolean remote) {
        if (source instanceof ProjectileDamageSource && (source.getSource() instanceof PersistentProjectileEntity projectileEntity)) {
            if (projectileEntity.getOwner() != null) {
                Entity owner = projectileEntity.getOwner();
                double targetX = owner.getX();
                double targetY = owner.getY();
                double targetZ = owner.getZ();

                double entityX = target.getX();
                double entityY = target.getY();
                double entityZ = target.getZ();

                double diffX = entityX - targetX;
                double diffY = entityY - targetY;
                double diffZ = entityZ - targetZ;

                projectileEntity.addVelocity(diffX, diffY, diffZ);
            } else {
                projectileEntity.setOwner(target);
                projectileEntity.setVelocity(10, projectileEntity.getVelocity().getY(), 10);
            }
            return false;
        }
        return true;
    }
}
